package com.dashboard.elasticsearch.impl;

import com.dashboard.common.utils.R;
import com.dashboard.elasticsearch.AbstractElasticsearchOperatorService;
import com.dashboard.enums.IndexEnum;
import com.dashboard.vo.QueryVO;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * @author xuxiao
 */
@Service
public class SimpleQueryElasticsearchService extends AbstractElasticsearchOperatorService {


    @Override
    public SearchRequest buildSearchRequest(QueryVO queryVO) {
        SearchRequest searchRequest = new SearchRequest();
        queryVO.setTitleId("1");
        queryVO.setTopicId("1");
        searchRequest.indices(IndexEnum.TEST.getIndexName());
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder queryBuilder = new BoolQueryBuilder();
        Map<String, Object> introspector = getIntrospector(queryVO);
        for ( Map.Entry<String, Object> inner :introspector.entrySet()) {
            queryBuilder.must(QueryBuilders.termQuery(inner.getKey(),inner.getValue()));
        }
        searchSourceBuilder.query(queryBuilder);
        searchRequest.source(searchSourceBuilder);
        return searchRequest ;
    }

    @Override
    public R parseElasticsearchResult(SearchResponse searchResponse) {
        return R.ok();
    }
}
